package com.sun.kamun.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sun.kamun.entity.*;
import com.sun.kamun.service.CategoryService;
import com.sun.kamun.service.ProductDtoService;
import com.sun.kamun.service.ProductService;
import com.sun.kamun.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/product")
@Slf4j
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private UserService userService;

    @Autowired
    private ProductDtoService productDtoService;

    /**
     * 查询全部商品
     */
    @GetMapping("/findAll")
    public String getAllProduct(Model model) {

        List<Product> productlist = productService.list();
        model.addAttribute("productlist", productlist);

        List<Category> categoryList = categoryService.list();
        model.addAttribute("categoryList", categoryList);

        return "productlist";
    }


    /**
     * 按条件组合查询商品
     */
    @GetMapping("/getProductByCondition")
    public String getProductByCondition(@RequestParam(value = "categoryId", required = false) int categoryId,
                                        @RequestParam(value = "productName", required = false) String productName,
                                        Model model) {
        try {
            LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
            if (categoryId != 0) {
                wrapper.eq(Product::getProductCategoryId, categoryId);
            }
            if (productName != null && !productName.isEmpty()) {
                wrapper.like(Product::getProductName, productName);
            }

            // 查询符合条件的商品列表
            List<Product> productList = productService.list(wrapper);

            // 将商品列表传递给视图
            model.addAttribute("productlist", productList);

            return "productlist";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

    /**
     * 用户---新增商品
     */
    @PostMapping(value = "/productInsertByUser",produces = "text/html;charset=UTF-8")
    public String insertProductByUser(HttpSession session, @RequestBody Product productAdd) {
        Product product=new Product();
        //先获取当前用户的登录信息
        User user = (User) session.getAttribute("user");
        // 判断用户是否登录
        if (user == null) {
            return "userlogin"; // 返回登录页面或错误提示
        }
        LambdaQueryWrapper<User> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(User::getUserName,user.getUserName());
        User one = userService.getOne(wrapper);
        // 用户已登录，可以继续执行新增商品的逻辑
        product.setProductCategoryId(productAdd.getProductCategoryId());
        product.setProductLeft(productAdd.getProductLeft());
        product.setProductName(productAdd.getProductName());
        product.setProductPrice(productAdd.getProductPrice());
        product.setProductUserId(one.getUserId());

        boolean save = productService.save(product);
        if (save) {
            return "productmanager";
        } else {
            return "error";
        }
    }

    /**
     * 用户---商品管理
     */
    @GetMapping("/productmanager")
    public String showProductManager(HttpSession session, Model model) {
        // 先获取当前用户的登录信息
        User user = (User) session.getAttribute("user");
        // 判断用户是否登录
        if (user == null) {
            return "userlogin"; // 如果用户未登录，则重定向到登录页面
        }
        //根据当前用户username查询用户的真实姓名
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getUserName, user.getUserName());
        User one = userService.getOne(lqw);
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Product::getProductUserId, one.getUserId());
        // 获取当前用户的商品列表
        List<Product> productList = productService.list(wrapper);
        // 获取商品分类信息
        List<Category> categoryList = categoryService.list();
        String getuserRealname = one.getUserRealname();
        log.info("当前用户信息为: " + one);
        // 将商品列表、商品分类列表和当前用户的真实姓名存入Model中，以便在Thymeleaf模板中使用
        model.addAttribute("products", productList);
        model.addAttribute("categories", categoryList);
        model.addAttribute("getuserRealname", getuserRealname);
        return "productmanager";
    }


}
